package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.FacilitiesFileDao;
import cn.topevery.garden.entity.po.FacilitiesFile;
import cn.topevery.garden.entity.query.FacilitiesFileQuery;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-06
 */
@Repository
public class FacilitiesFileDaoImpl extends BaseDaoImpl<FacilitiesFile> implements FacilitiesFileDao {
    @Override
    public <P extends DbCsp> List<FacilitiesFile> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<FacilitiesFile> listPage(FacilitiesFileQuery query) {
        String table = "t_facilities_file tff";

        String columns = "tff.*";
		int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tff.c_id desc");
        sqlQueryMate
                .where(true, "tff.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, FacilitiesFile.class);
    }

    @Override
    public void delByMainId(String userId, Integer mainId) {
        String table = "t_facilities_file tff";

        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate(table);
        sqlUpdateMate
                .setColumnValue(true, "tff.c_db_status", 1)
                .setColumnValue(true, "tff.c_db_last_update_id", userId)
                .where(true, "tff.c_main_id", ScriptConditionEnum.Eq, mainId);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public List<FacilitiesFile> getByMainId(Integer mainId) {
        String table = "t_facilities_file tff";

        String columns = "tff.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tff.c_id desc");
        sqlQueryMate
                .where(true, "tff.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "tff.c_main_id", ScriptConditionEnum.Eq, mainId);

        return selectList(sqlQueryMate, FacilitiesFile.class);
    }
}
